package org.zhengquan.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zhengquan.common.JsonData;
import org.zhengquan.pojo.SysUser;
import org.zhengquan.service.*;
import org.zhengquan.util.StringUtil;
import org.zhengquan.vo.RoleVO;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhengquan
 * @date 2017/11/3
 */
@Controller
@RequestMapping("/sys/role/")
@Slf4j
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysTreeService sysTreeService;

    @Autowired
    private SysRoleAclService sysRoleAclService;

    @Autowired
    private SysRoleUserService sysRoleUserService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("role.page")
    public ModelAndView page() {
        return new ModelAndView("role");
    }

    @RequestMapping("save.json")
    @ResponseBody
    public JsonData save(RoleVO roleVO) {
        sysRoleService.save(roleVO);
        return JsonData.success();
    }

    @RequestMapping("list.json")
    @ResponseBody
    public JsonData list() {
        return JsonData.success(sysRoleService.selectList());
    }

    @RequestMapping("update.json")
    @ResponseBody
    public JsonData update(RoleVO roleVO) {
        sysRoleService.update(roleVO);
        return JsonData.success();
    }

    @RequestMapping("roleTree.json")
    @ResponseBody
    public JsonData roleTree(@RequestParam("roleId") int roleId) {
        return JsonData.success(sysTreeService.roleTree(roleId));
    }

    @RequestMapping("changeAcls.json")
    @ResponseBody
    public JsonData changeAcls(@RequestParam("roleId") int roleId, @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) {
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        sysRoleAclService.changeRoleAcls(roleId, aclIdList);
        return JsonData.success();
    }

    @RequestMapping("changeUsers.json")
    @ResponseBody
    public JsonData changeUsers(@RequestParam("roleId") int roleId, @RequestParam(value = "userIds", required = false, defaultValue = "") String userIds) {
        List<Integer> userIdList = StringUtil.splitToListInt(userIds);
        sysRoleUserService.changeRoleUsers(roleId, userIdList);
        return JsonData.success();
    }
    @RequestMapping("users.json")
    @ResponseBody
    public JsonData users(@RequestParam("roleId") int roleId) {
        List<SysUser> selectedUserList = sysRoleUserService.selectListByRoleId(roleId);
        List<SysUser> allUserList = sysUserService.selectAllUsers();
        List<SysUser> unSelectedUserList = Lists.newArrayList();
        Set<Integer> selectUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
            if (sysUser.getState() == 1 && !selectUserIdSet.contains(sysUser.getId())) {
                unSelectedUserList.add(sysUser);
            }
        }
        selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getState() == 1).collect(Collectors.toList());
        Map<String, List<SysUser>> map = Maps.newHashMap();
        map.put("selected", selectedUserList);
        map.put("unselected", unSelectedUserList);
        return JsonData.success(map);
    }
}